
***********;
*** DME ***;
***********;
%macro dme(startyr, endyr);
%do i= &startyr %to &endyr;
	%IF &i>2013 %then %let k= k;
	%IF &i<=2013 %then %let k= j;
	*DME-Line;
	DATA temp1;	set cms.dme_line_&k._&i;  
		if NCH_CLM_TYPE_CD="82"; if LINE_PRCSG_IND_CD="A"; carrier = 0;
		keep BENE_ID CLM_THRU_DT LINE_SRVC_CNT HCPCS_CD HCPCS_1ST_MDFR_CD HCPCS_2ND_MDFR_CD LINE_ALOWD_CHRG_AMT LINE_NCH_PMT_AMT PRVDR_NPI carrier;
	RUN;
	*Carrier-Line;
	DATA temp2; set cms.bcarrier_line_&k._&i; 
		if NCH_CLM_TYPE_CD="72"; if LINE_PRCSG_IND_CD="A";
		rename PRF_PHYSN_NPI = PRVDR_NPI; carrier = 1;
		keep BENE_ID CLM_THRU_DT LINE_SRVC_CNT HCPCS_CD HCPCS_1ST_MDFR_CD HCPCS_2ND_MDFR_CD LINE_ALOWD_CHRG_AMT LINE_NCH_PMT_AMT PRF_PHYSN_NPI carrier;
	RUN;

	* Append all years;
	DATA dme_temp; set temp1 temp2; RUN;
	%IF &i=&startyr %then %do;
		DATA dme_total; set dme_temp; RUN;
	%END;
	%ELSE %do;
		PROC append base=dme_total data=dme_temp force; RUN;
	%END;
%END;
%mend dme;
%dme(2009, 2015);
PROC datasets library=work; delete dme_temp temp1 temp2; RUN;


****************************;
* HCPCS - Mod1 - Mod2 List *;
****************************;
PROC SQL;
	CREATE table hcpcs_list as
	SELECT HCPCS_CD, HCPCS_1ST_MDFR_CD, HCPCS_2ND_MDFR_CD, count(LINE_SRVC_CNT) as n_claim
	FROM dme_total
	GROUP by HCPCS_CD, HCPCS_1ST_MDFR_CD, HCPCS_2ND_MDFR_CD;
QUIT;

DATA hcpcs_list; set hcpcs_list;
	if hcpcs_1st_mdfr_cd in ("NU","RR","UE","MS","KL","BA","KC") then temp1 = hcpcs_1st_mdfr_cd;
	if hcpcs_2nd_mdfr_cd in ("NU","RR","UE","MS","KL","BA","KC") then temp2 = hcpcs_2nd_mdfr_cd;
	if temp1="NU" and temp2 in ("RR","UE","MS","KL","BA","KC","") then do; Mod1 = "NU"; Mod2 = temp2; end;
	if temp2="NU" and temp1 in ("RR","UE","MS","KL","BA","KC","") then do; Mod1 = "NU"; Mod2 = temp1; end;
	if temp1="RR" and temp2 in ("UE","MS","KL","BA","KC","") then do; Mod1 = "RR"; Mod2 = temp2; end;
	if temp2="RR" and temp1 in ("UE","MS","KL","BA","KC","") then do; Mod1 = "RR"; Mod2 = temp1; end;
	if temp1="UE" and temp2 in ("MS","KL","BA","KC","") then do; Mod1 = "UE"; Mod2 = temp2; end;
	if temp2="UE" and temp1 in ("MS","KL","BA","KC","") then do; Mod1 = "UE"; Mod2 = temp1; end;
	if temp1="MS" and temp2 in ("KL","BA","KC","") then do; Mod1 = "MS"; Mod2 = temp2; end;
	if temp2="MS" and temp1 in ("KL","BA","KC","") then do; Mod1 = "MS"; Mod2 = temp1; end;
	if temp1="KL" and temp2 in ("BA","KC","") then do; Mod1 = "KL"; Mod2 = temp2; end;
	if temp2="KL" and temp1 in ("BA","KC","") then do; Mod1 = "KL"; Mod2 = temp1; end;
	if temp1="BA" and temp2 in ("KC","") then do; Mod1 = "BA"; Mod2 = temp2; end;
	if temp2="BA" and temp1 in ("KC","") then do; Mod1 = "BA"; Mod2 = temp1; end;
	drop temp1 temp2;
RUN;

DATA HCPCS_CBP; set input.HCPCS_CBP; RUN;
DATA hcpcs_wMod1; set HCPCS_CBP; rename HCPCS = HCPCS_CD; if Mod1~="" and Mod2~=""; RUN;
PROC sort data=hcpcs_wMod1 NODUPKEY; by HCPCS_CD Mod1 Mod2; RUN;
DATA hcpcs_wMod2; set HCPCS_CBP; rename HCPCS = HCPCS_CD; if Mod1~="" and Mod2=""; drop Mod2; RUN;
PROC sort data=hcpcs_wMod2 NODUPKEY; by HCPCS_CD Mod1; RUN;
DATA hcpcs_wMod3; set HCPCS_CBP; rename HCPCS = HCPCS_CD; if Mod1="" and Mod2=""; drop Mod1 Mod2; RUN;
PROC sort data=hcpcs_wMod3 NODUPKEY; by HCPCS_CD; RUN;
DATA hcpcs_wMod4; set HCPCS_CBP; rename HCPCS = HCPCS_CD; keep HCPCS; RUN;
PROC sort data=hcpcs_wMod4 NODUPKEY; by HCPCS_CD; RUN;

* 1) HCPCS+Mod1+Mod2;
PROC sort data=hcpcs_list; by HCPCS_CD Mod1 Mod2; RUN;
DATA hcpcs_list_matched;
	merge hcpcs_list (in = incms) hcpcs_wMod1 (in = inhcpcs); by HCPCS_CD Mod1 Mod2; if incms; matched = inhcpcs;
	rename category = category_X; rename NMO = NMO_X; rename R1 = R1_X;	rename R2 = R2_X; rename R1RC = R1RC_X; drop note;
RUN;

* 2) HCPCS+Mod1;
PROC sort data=hcpcs_list_matched; by HCPCS_CD Mod1; RUN;
DATA hcpcs_list_matched;
	merge hcpcs_list_matched (in = incms) hcpcs_wMod2 (in = inhcpcs); by HCPCS_CD Mod1; if incms; match2 = inhcpcs;
	if match2=1 and matched=0 then do; category_X = category; NMO_X = NMO; R1_X = R1; R2_X = R2; R1RC_X = R1RC; Mod2 = ""; matched = 1; end;
	drop category NMO R1 R2 R1Rc Note_R1 match2; 
RUN;

* 3) HCPCS;
PROC sort data=hcpcs_list_matched; by HCPCS_CD; RUN;
DATA hcpcs_list_matched;
	merge hcpcs_list_matched (in = incms) hcpcs_wMod3 (in = inhcpcs); by HCPCS_CD; if incms; match3 = inhcpcs;
	if match3=1 and matched=0 then do; category_X = category; NMO_X = NMO; R1_X = R1; R2_X = R2; R1RC_X = R1RC; Mod1 = ""; Mod2 = ""; matched = 1; end;
	drop category NMO R1 R2 R1Rc Note_R1 match3; 
RUN;
PROC sort data = hcpcs_list_matched; by DESCENDING n_claim; RUN;
PROC datasets library=work; delete hcpcs_wMod1 hcpcs_wMod2 hcpcs_wMod3; RUN;


*********************;
* DME_total cleaned *;
*********************;
PROC sort data=dme_total; by HCPCS_CD HCPCS_1ST_MDFR_CD HCPCS_2ND_MDFR_CD; RUN;
PROC sort data = hcpcs_list_matched; by HCPCS_CD HCPCS_1ST_MDFR_CD HCPCS_2ND_MDFR_CD; RUN;
DATA dme_total_matched; 
	merge dme_total (in = incms) hcpcs_list_matched (in = inhcpcs); by HCPCS_CD HCPCS_1ST_MDFR_CD HCPCS_2ND_MDFR_CD; if incms; 
	drop HCPCS_1ST_MDFR_CD HCPCS_2ND_MDFR_CD n_claim matched; 
RUN;

